package com.sinvo.oa.controller;

import com.alibaba.fastjson2.JSON;
import com.sinvo.oa.entity.model.system.SysUser;
import com.sinvo.oa.entity.vo.system.LoginVo;
import com.sinvo.oa.entity.vo.system.RouterVo;
import com.sinvo.oa.result.R;
import com.sinvo.oa.service.SysMenuService;
import com.sinvo.oa.service.SysUserService;
import com.sinvo.oa.util.Base64Utils;
import com.sinvo.oa.util.JwtUtils;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description 登录控制器
 */
@Api(tags = "后台登录管理")
@RestController
@RequestMapping("admin/system/index")
public class IndexController {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysMenuService sysMenuService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 登录 引入spring-security 此方法不再经过
     *
     * @param loginVo username password
     * @return R
     */
    @PostMapping("login")
    public R login(@RequestBody LoginVo loginVo) {
        String token = sysUserService.verify(loginVo);
        //返回
        Map<String, Object> map = new HashMap<>();
        map.put("token", token);
        return R.success(map);
    }

    /**
     * 获取用户信息
     *
     * @return
     */
    @GetMapping("info")
    public R info(HttpServletRequest request) throws UnsupportedEncodingException {
        //获取token
        String token = request.getHeader("token");
        //token解析数据
        Long userId = JwtUtils.getUserId(token);
        //根据userId获取数据
        SysUser sysUser = sysUserService.getById(userId);
        //根据根据userId查询其操作菜单(router)
        List<RouterVo> routerVoList = sysMenuService.findUserMenuListByUserId(userId);
        //根据userId查询可操作按钮
        List<String> permsList = sysMenuService.findUserPermsByUserId(userId);
        String avatar = sysUser.getHeadUrl();
        if (avatar == null) {
            avatar = this.getClass().getClassLoader().getResource("avatar/default.jpg").getPath();
            avatar = URLDecoder.decode(avatar, "utf-8");
        }
        Map<String, Object> map = new HashMap<>();
        //封装数据返回
        map.put("name", sysUser.getName());
        map.put("avatar", Base64Utils.ImageToBase64Encode(avatar));
        map.put("routers", routerVoList);
        map.put("buttons", permsList);
        System.out.println("路由数据：" + JSON.toJSON(routerVoList));
        return R.success(map);
    }

    /**
     * 退出
     *
     * @return
     */
    @PostMapping("logout")
    public R logout(HttpServletRequest request) {
        //获取token
        String token = request.getHeader("token");
        //解析用户名
        String username = JwtUtils.getUsername(token);
        //redis移除用户的权限数据
        redisTemplate.delete("oa_" + username);
        return R.success();
    }
}
